package com.nondo.dean.platform.controller.api.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Freight;
import com.nondo.dean.platform.model.result.FreightResult;
import com.nondo.dean.platform.service.FreightService;

/**
 * 区域运费折扣管理后台接口
 * @author zlj
 * @date 2018年4月3日
 */
@RestController("apiAdminFreight")
@RequestMapping("/{v}/admin/freight")
public class FreightController {
    
    @Autowired
    FreightService freightService;
    
    /**
     * 查看区域运费折扣详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<FreightResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<FreightResult> view = new View<FreightResult>(20000, "获取数据成功");
        
        FreightResult freightResult = new FreightResult();
        if (id != null) {
            Freight freightDb = this.freightService.selectFreight(id);
            freightResult = this.freightService.getFreightDetail(freightDb);
        }

        view.setData(freightResult);
        return view;
    }
    
    /**
     * 添加区域运费折扣
     * @param request
     * @param response
     * @param freight 区域运费折扣信息 {@link Freight}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Freight> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Freight freight, BindingResult result) {
        View<Freight> view = new View<Freight>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Freight freightDb = this.freightService.getFreightByZoneId(freight.getZoneId());
            if (freightDb != null) {
                return View.of500Error("区域运费折扣已存在");
            }
            this.freightService.addFreight(freight);
        }
        return view;
    }

    /**
     * 更新区域运费折扣
     * @param request
     * @param response
     * @param freight 区域运费折扣信息 {@link Freight}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Freight> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Freight freight, BindingResult result) {
        View<Freight> view = new View<Freight>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Freight freightDb = this.freightService.getFreightByZoneId(freight.getZoneId());
            if (freightDb.getId() != freight.getId()) {
                return View.of500Error("区域运费折扣已存在");
            }
            BeanUtils.copy(freight, freightDb);
            this.freightService.updateFreight(freightDb);
        }

        return view;
    }

    /**
     * 获取区域运费折扣分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<FreightResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Freight> query) {
        View<Page<FreightResult>> view = new View<Page<FreightResult>>(20000, "获取列表成功");

        Freight freight = query.getData();

        Page<FreightResult> page = new Page<FreightResult>(query.getPage(), query.getLimit(), freightService.count(freight));
        
        List<Freight> freightList = freightService.getFreightList(query.getOffset(), query.getLimit(), freight);
        
        List<FreightResult> resultList = new ArrayList<FreightResult>();
        
        for (Freight freightDb : freightList) {
            FreightResult freightResult = this.freightService.getFreightDetail(freightDb);
            resultList.add(freightResult);
        }
        
        page.setContent(resultList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除区域运费折扣
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Freight freight = freightService.selectFreight(id);
        freight.setDelFlag(String.valueOf(1));
        this.freightService.updateFreight(freight);

        return view;
    }
    
}